package kr.co.dongpang.android;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.squareup.picasso.Picasso;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup.MarginLayoutParams;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

public class SmGoodImageDownCustomDialog extends Activity {

	String goodcd;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		goodcd = getIntent().getStringExtra("goodcd");

		setContentView(R.layout.sm_good_imagedown_dialog);

		new detailTask().execute(null, null, null);
	}

	class detailTask extends AsyncTask<Void, Void, Void> {

		int count;
		String color;

		@Override
		protected Void doInBackground(Void... params) {
			// TODO Auto-generated method stub

			JSONObject json = JSONfunctions.getJSONfromURL("http://dongpang.co.kr/android/dongpang.v01/sm_detail_imagedown.php?goodcd=" + goodcd);

			try {
				JSONArray dongpang = json.getJSONArray("dongpang");
				JSONObject jobject = dongpang.getJSONObject(0);
				count = Integer.parseInt(jobject.getString("count"));
				color = jobject.getString("color");
			} catch (JSONException e) {
				Log.e("log_tag", "Error Parsing data" + e.toString());
			}

			return null;
		}

		protected void onPostExecute(Void result) {

			LinearLayout colorarea = (LinearLayout) findViewById(R.id.colorArea); 
			colorarea.setOrientation(LinearLayout.HORIZONTAL);
			String[] colorStrings = color.split(";");
			for (int i = 0; i < count; i++) {
				String imgXS = "http://www.dongpang.co.kr/images/userdif/goodsColor/" + goodcd + "-" + colorStrings[i] + "S.jpg";
				final String imgXL = "http://www.dongpang.co.kr/images/userdif/goodsColor/" + goodcd + "-" + colorStrings[i] + "L.jpg";
				final String fileName =  goodcd + "-" + colorStrings[i];
				final ImageView img = new ImageView(SmGoodImageDownCustomDialog.this);
				img.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));
				img.getLayoutParams().height = 120;
				img.getLayoutParams().width = 100;
				MarginLayoutParams marginParams = new MarginLayoutParams(img.getLayoutParams());
				marginParams.setMargins(10, 5, 10, 5);
				LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(marginParams);
				img.setLayoutParams(layoutParams);
				Picasso.with(SmGoodImageDownCustomDialog.this).load(imgXS).into(img); 

				img.setOnClickListener(new View.OnClickListener() {

					@Override
					public void onClick(View v) {
						new imageDownTask().execute(imgXL, fileName, null);
					}
				});

				colorarea.addView(img);

			}

			super.onPostExecute(result);

		}

	}
	
	class imageDownTask extends AsyncTask<String, String, Void>{
		
		String imgXL; 
		String fileName;

		@Override
		protected Void doInBackground(String... params) {
			// TODO Auto-generated method stub
			imgXL = params[0];
			fileName = params[1];
			
			Log.d("","imgXL::"+imgXL);
			Log.d("","imgXL::"+fileName);
			
			URL url;
			try {
				url = new URL(imgXL);
				url.openConnection();
				Bitmap bitmap = BitmapFactory.decodeStream(url.openStream());
				String path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/"+fileName+".jpg";
				try {
					File file = new File(path);
					bitmap.compress(CompressFormat.JPEG, 100, new FileOutputStream(file));
					sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(file)));
				} catch (FileNotFoundException e) {
					e.printStackTrace();
				}
			} catch (MalformedURLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}			
			
			return null;
		}
		
		@Override
		protected void onPostExecute(Void result) {
			// TODO Auto-generated method stub
			
			Toast.makeText(SmGoodImageDownCustomDialog.this, fileName+" download...", Toast.LENGTH_LONG).show();
			
			super.onPostExecute(result);
		}
		
	}
	

}
